﻿using EONFULLSIN2.Models.Entity.Device;
using EONFULLSIN2.Services.DB;
using EONFULLSIN2.Services.Message;
using EONFULLSIN2.ViewModelBase;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.ViewModel {
    public class DataHistoryViewModel : NavigationViewModel {
        public DataHistoryViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate)
            : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {

        }

        #region Props
        private string _searchText;

        public string SearchText {
            get { return _searchText; }
            set { _searchText = value; base.RaisePropertyChanged(); }
        }

        private ObservableCollection<DeviceRunRecord> _dataList = new ObservableCollection<DeviceRunRecord>();

        public ObservableCollection<DeviceRunRecord> DataList {
            get { return _dataList; }
            set { _dataList = value; base.RaisePropertyChanged(); }
        }
        #endregion

        #region Commands

        public DelegateCommand SearchCommand => new DelegateCommand(() => {
            LoadData();
        });

        #endregion


        protected override void OnViewLoaded() {
            LoadData();
            //base.OnViewLoaded();
        }


        private void LoadData() {
            var list = _mainDbOperate.GetFreeSql().Select<DeviceRunRecord>()
                 .WhereIf(!string.IsNullOrEmpty(SearchText), o => o.Description.Contains(SearchText))
                 .ToList();
            DataList.AddRange(list);
        }

    }
}
